package cn.stylefeng.guns.modular.course.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.entity.Course;

import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.guns.modular.course.service.CourseService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.apache.poi.hssf.record.chart.CategorySeriesAxisRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 控制器
 *
 * @author typ
 * @Date 2021-04-13 23:21:53
 */
@Controller
@RequestMapping("/course")
public class CourseController extends BaseController {

    private String PREFIX = "/course";

    @Autowired
    private CourseService courseService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 跳转到主页面
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/course.html";
    }

    /**
     * 新增页面
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/course_add.html";
    }

    /**
     * 编辑页面
     *
     * @author typ
     * @Date 2021-04-13
            */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/course_edit.html";
    }

    /**
     * 新增接口
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(Course courseParam) {
        //获取父id
        Category category=categoryService.getById(courseParam.getCategoryId());
        courseParam.setCategoryPid(category.getPid());
        this.courseService.add(courseParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(Course courseParam) {
        Category category=categoryService.getById(courseParam.getCategoryId());
        courseParam.setCategoryPid(category.getPid());
        this.courseService.update(courseParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(Course courseParam) {
        this.courseService.delete(courseParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author typ
     * @Date 2021-04-13
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(Course courseParam) {
        Course detail = this.courseService.getById(courseParam.getId());
        Category category=categoryService.getById(detail.getCategoryId());
        Category parentCategory=categoryService.getById(detail.getCategoryPid());
        detail.setCategoryName(category.getName()+"-"+parentCategory.getName());

        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author typ
     * @Date 2021-04-13
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(Course courseParam) {
        return this.courseService.findPageBySpec(courseParam);
    }


    /*
    设置热门
     */
    @RequestMapping("/setHot")
    @ResponseBody
    public ResponseData setHot(int id,Integer isHot) {
        Course detail = this.courseService.getById(id);
        detail.setIsHot(isHot);
        courseService.update(detail);

        return ResponseData.success(detail);
    }
}


